package requrest

import (
	"errors"
	"go-project/dto"
	"go-project/models"

	"github.com/gin-gonic/gin"
)

type AdminSearchRequest struct {
	Pagination Pagination
	Username   string `form:"username"`
	Email      string `form:"email"`
	Phone      string `form:"phone"`
}

type AdminInsertRequest struct {
	Username string `json:"username" binding:"required,username_unique" required_msg:"用户名不能为空" username_unique_msg:"该用户名已存在"`
	Email    string `json:"email" binding:"required,email,email_unique" required_msg:"邮箱不能为空" email_unique_msg:"该邮箱已存在"`
	Phone    string `json:"phone" binding:"required,phone,phone_unique" required_msg:"手机号不能为空" phone_unique_msg:"该手机号已存在" phone_msg:"手机格式不正确"`
	Role     string `json:"role" binding:"required" required_msg:"角色不能为空"`
	Password string `json:"password" binding:"required,min=6,max=16" required_msg:"密码不能为空" min_msg:"密码长度不能小于6" max_msg:"密码长度不能大于16"`
	Address  string `json:"address" binding:"required" required_msg:"地址不能为空"`
	Province string `json:"province" binding:"required" required_msg:"省份不能为空"`
	City     string `json:"city" binding:"required" required_msg:"市不能为空"`
	County   string `json:"county" binding:"required" required_msg:"区/县不能为空"`
	Desc     string `json:"desc"`
	dto.ValidateBindRequest
}

func (admin_in_req *AdminInsertRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(admin_in_req); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", admin_in_req)
		return err
	}
	return nil
}
func (s *AdminInsertRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	admin, ok := data.(*models.Admin)
	if ok {
		admin.Password = s.Password
		admin.Email = s.Email
		admin.Phone = s.Phone
		admin.Username = s.Username
		admin.Address = s.Address
		admin.Desc = s.Desc
		admin.Province = s.Province
		admin.County = s.County
		admin.City = s.City
		return nil
	}
	s.Errors = errors.New("数据类型不正确，数据必须是models.Admin类型")
	return &s.ValidateBindRequest

}

type AdminUpdateRequest struct {
	AdminId  uint   `json:"id"  binding:"required" required_msg:"id不能为空"`
	Username string `json:"username" binding:"required,username_unique=update" required_msg:"用户名不能为空" username_unique_msg:"该用户名已存在"`
	Email    string `json:"email" binding:"required,email,email_unique=update" required_msg:"邮箱不能为空" email_unique_msg:"该邮箱已存在"`
	Role     string `json:"role" binding:"required" required_msg:"角色不能为空"`
	Phone    string `json:"phone" binding:"required,phone,phone_unique=update" required_msg:"手机号不能为空" phone_unique_msg:"该手机号已存在" phone_msg:"手机格式不正确"`
	Password string `json:"password" binding:"required,min=6,max=16" required_msg:"密码不能为空" min_msg:"密码长度不能小于6" max_msg:"密码长度不能大于16"`
	Address  string `json:"address" binding:"required" required_msg:"地址不能为空"`
	Province string `json:"province" binding:"required" required_msg:"省份不能为空"`
	City     string `json:"city" binding:"required" required_msg:"市不能为空"`
	County   string `json:"county" binding:"required" required_msg:"区/县不能为空"`
	Desc     string `json:"desc"`
	dto.ValidateBindRequest
}

func (admin_up_req *AdminUpdateRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBind(admin_up_req); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", admin_up_req)
		return err
	}
	return nil
}
func (s *AdminUpdateRequest) Generate(data interface{}) (vab_req *dto.ValidateBindRequest) {
	admin, ok := data.(*models.Admin)
	if ok {
		admin.ID = s.AdminId
		admin.Password = s.Password
		admin.Email = s.Email
		admin.Phone = s.Phone
		admin.Username = s.Username
		admin.Address = s.Address
		admin.Desc = s.Desc
		admin.Province = s.Province
		admin.County = s.County
		admin.City = s.City
		return nil
	}
	s.Errors = errors.New("数据类型不正确，数据必须是models.Admin类型")
	return &s.ValidateBindRequest

}

type AdminDistributeRoleRequest struct {
	AdminId uint   `json:"id"  binding:"required" required_msg:"id不能为空"`
	Role    string `json:"role" binding:"required" required_msg:"角色不能为空"`
	dto.ValidateBindRequest
}

func (adminDistributeRoleRequest *AdminDistributeRoleRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBind(adminDistributeRoleRequest); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", adminDistributeRoleRequest)
		return err
	}
	return nil
}
